const jwt = require('jsonwebtoken')
const secret = 'token-the-year-of-maple' // 秘钥
const Token = {
	//生成token令牌
	encrypt: function (data) {
		//data加密数据，token-secret, 秘钥, time过期时间1天
		let token = jwt.sign(data, secret, { expiresIn: 1000 * 60 * 60 * 24 })
		return token
	},

	// 验证token
	decrypt: function (token) {
		try {
			let data = jwt.verify(token, secret)
			console.log('解密token中' +data)
			return {
				token: true,
				id: data.id,
			}
		} catch (e) {
			return {
				token: false,
				data: e,
			}
		}
	},
}

module.exports = Token
